package com.inossem.warehousestorage_inossem.activity.change_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.extra.ChangeStorageExtra;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStorageOverviewActivity;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.ChangeStorageManageBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.changeStorage.ChangeStorageManageRequest;
import com.inossem.warehousestorage_inossem.request.changeStorage.ChangeStorageTargetFactoryListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.ChangeStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

public class ChangeStorageManageActivity extends BaseActivity {

    TextView title, factory, inventoryAddress, moveType, specialInventory, nextStep;

    private TextView factoryValue, inventoryAddressValue, moveTypeValue, warehouseNumber, businessTypeValue;

    private PercentRelativeLayout factoryHide, moveTypeHide;

    private List<ChangeStorageManageBean.DataBean.FactoryListBean> mFactoryList;

    private List<ChangeStorageManageBean.DataBean.FactoryListBean> mTargetFactoryList;

    private List<ChangeStorageManageBean.DataBean.MoveTypeListBean> mMoveTypeList;

    private String mIsFocus;

    private boolean mFlag = false;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_storage_manage);
        initView(ChangeStorageManageActivity.this);
        SPUtils.removeChangeStorageInformation(ChangeStorageManageActivity.this);
        initData(ChangeStorageManageActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        factory = (TextView) activity.findViewById(R.id.factory);
        inventoryAddress = (TextView) activity.findViewById(R.id.inventoryAddress);
        moveType = (TextView) activity.findViewById(R.id.moveType);
        specialInventory = (TextView) activity.findViewById(R.id.specialInventory);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        factoryValue = (TextView) activity.findViewById(R.id.factoryValue);
        inventoryAddressValue = (TextView) activity.findViewById(R.id.inventoryAddressValue);
        moveTypeValue = (TextView) activity.findViewById(R.id.moveTypeValue);
        warehouseNumber = (TextView) activity.findViewById(R.id.warehouseNumber);
        businessTypeValue = (TextView) activity.findViewById(R.id.businessTypeValue);

        factoryHide = (PercentRelativeLayout) activity.findViewById(R.id.factoryHide);
        moveTypeHide = (PercentRelativeLayout) activity.findViewById(R.id.moveTypeHide);

    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_change_storage_manage_title));
        ActivityUtils.initBackAndFunction(activity, "主页", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getConfirmDialog(ChangeStorageManageActivity.this, "退出后将清空所填信息", new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        finish();
                    }
                }).show();
            }
        });
        getExtra(activity);
        if (mFlag) {
//            Intent intent = new Intent(activity, UpFrame_ManageActivity.class);
//            startActivity(intent);
//            finish();
        } else {
            getData(activity);
        }
    }

    private void getExtra(Activity activity) {
        mFlag = activity.getIntent().getBooleanExtra(ChangeStorageExtra.FLAG, false);
    }

    private void getData(Context context) {
        factoryHide.setVisibility(View.GONE);
        moveTypeHide.setVisibility(View.GONE);
        ChangeStorageManageRequest.request(context, new GsonBeanListener<ChangeStorageManageBean>() {
            @Override
            public void onSuccess(ChangeStorageManageBean bean) {
                mFactoryList = bean.getData().getFactoryList();
                mMoveTypeList = bean.getData().getMoveTypeList();
                mIsFocus = bean.getData().getIsFocus();
                initSpinner(context);
            }
        });

        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String checkResult = check(context);
                if (checkResult == null) {
                    ChangeStorageTargetFactoryListRequest.request(context, factoryValue.getText().toString(), businessTypeValue.getText().toString(), new GsonBeanListener<ChangeStorageManageBean>() {
                        @Override
                        public void onSuccess(ChangeStorageManageBean bean) {
                            mTargetFactoryList = bean.getData().getTargetFactoryList();
                            for (int i = 0; i < mTargetFactoryList.size(); i++) {
                                ChangeStorageManageBean.DataBean.FactoryListBean factoryItem = mTargetFactoryList.get(i);
                                List<ChangeStorageManageBean.DataBean.InventoryAddressListBean> inventoryAddressList = factoryItem.getInventoryAddressList();
                                for (int j = 0; j < inventoryAddressList.size(); j++) {
                                    ChangeStorageManageBean.DataBean.InventoryAddressListBean inventoryAddressItem = inventoryAddressList.get(j);
                                }
                            }
                            updateInfo(context);
                            Intent intent = new Intent(context, ChangeStorageMaterielListActivity.class);
                            startActivity(intent);
                        }
                    });
                } else {
                    Utils.middleToast(context, checkResult);
                }
            }
        });
    }

    private void showFactory(Context context, String text, String value, List<ChangeStorageManageBean.DataBean.InventoryAddressListBean> inventoryAddressList) {
        factory.setText(value + " - " + text);
        factoryValue.setText(value);
        resetInventorAddress(context);
        factoryHide.setVisibility(View.VISIBLE);
        inventoryAddress.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getChangeStorageInventoryAddressDialog(context, inventoryAddressList, new DialogUtils.ChangeStorageInventoryAddressListener() {
                    @Override
                    public void onSelect(Dialog dialog, String text, String value, String warehouseNumber) {
                        if (!value.equals(inventoryAddressValue.getText().toString())) {
                            if (ChangeStorageUtils.hasMaterielList(context)) {
                                DialogUtils.getConfirmDialog(context, "修改库存地点将清除之前添加的物料", new DialogUtils.ConfirmDialogListener() {
                                    @Override
                                    public void onConfirm(Dialog dialog) {
                                        dialog.cancel();
                                        ChangeStorageUtils.deleteMaterielList(context);
                                        showInventoryAddress(text, value, warehouseNumber);
                                    }
                                }).show();
                            } else {
                                showInventoryAddress(text, value, warehouseNumber);
                            }

                        }
                    }
                }).show();
            }
        });
    }

    private void showInventoryAddress(String text, String value, String warehouseNumberStr) {
        inventoryAddress.setText(value + " - " + text);
        inventoryAddressValue.setText(value);
        warehouseNumber.setText(warehouseNumberStr);
    }

    private void showMoveType(String moveTypeStr, String moveTypeDescription, String specialInventoryStr, String businessType) {
        moveType.setText(moveTypeStr + " - " + moveTypeDescription);
        moveTypeValue.setText(moveTypeStr);
        specialInventory.setText(specialInventoryStr);
        businessTypeValue.setText(businessType);
        moveTypeHide.setVisibility(View.VISIBLE);
    }

    private void initSpinner(Context context) {
        factory.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getChangeStorageFactoryDialog(context, mFactoryList, new DialogUtils.ChangeStorageFactoryListener() {
                    @Override
                    public void onSelect(Dialog dialog, String text, String value, List<ChangeStorageManageBean.DataBean.InventoryAddressListBean> inventoryAddressList) {
                        if (!value.equals(factoryValue.getText().toString())) {
                            if (ChangeStorageUtils.hasMaterielList(context)) {
                                DialogUtils.getConfirmDialog(context, "修改工厂将清除之前添加的物料", new DialogUtils.ConfirmDialogListener() {
                                    @Override
                                    public void onConfirm(Dialog dialog) {
                                        dialog.cancel();
                                        ChangeStorageUtils.deleteMaterielList(context);
                                        showFactory(context, text, value, inventoryAddressList);
                                    }
                                }).show();
                            } else {
                                showFactory(context, text, value, inventoryAddressList);
                            }
                        }
                    }
                }).show();
            }
        });
        moveType.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getChangeStorageMoveTypeDialog(context, mMoveTypeList, new DialogUtils.ChangeStorageMoveTypeListener() {
                    @Override
                    public void onSelect(Dialog dialog, String moveTypeStr, String moveTypeDescription, String specialInventoryStr, String businessType) {
                        if (!moveTypeStr.equals(moveType.getText().toString())) {
                            if (ChangeStorageUtils.hasMaterielList(context)) {
                                DialogUtils.getConfirmDialog(context, "修改移动类型将清除之前添加的物料", new DialogUtils.ConfirmDialogListener() {
                                    @Override
                                    public void onConfirm(Dialog dialog) {
                                        dialog.cancel();
                                        ChangeStorageUtils.deleteMaterielList(context);
                                        showMoveType(moveTypeStr, moveTypeDescription, specialInventoryStr, businessType);
                                    }
                                }).show();
                            } else {
                                showMoveType(moveTypeStr, moveTypeDescription, specialInventoryStr, businessType);
                            }
                        }
                    }
                }).show();
            }
        });
    }

    private void resetInventorAddress(Context context) {
        inventoryAddress.setText(context.getResources().getString(R.string.activity_in_storage_other_index_inventory_address_hint));
        inventoryAddressValue.setText("");
        factoryHide.setVisibility(View.GONE);
    }

    private String check(Context context) {
        if ("".equals(factoryValue.getText().toString())) {
            return context.getResources().getString(R.string.activity_in_storage_other_index_factory_hint);
        }
        if ("".equals(inventoryAddressValue.getText().toString())) {
            return context.getResources().getString(R.string.activity_in_storage_other_index_inventory_address_hint);
        }
        if (context.getResources().getString(R.string.activity_in_storage_other_index_move_type_hint).equals(moveType.getText().toString())) {
            return context.getResources().getString(R.string.activity_in_storage_other_index_move_type_hint);
        }
        return null;
    }

    private void updateInfo(Context context) {
        ChangeStorageBean bean = ChangeStorageUtils.getChangeStorageBean(context);
        bean.setIsFocus(mIsFocus);
        bean.setFactoryCode(factoryValue.getText().toString());
        bean.setInventoryAddressCode(inventoryAddressValue.getText().toString());
        bean.setWarehouseNumber(warehouseNumber.getText().toString());
        bean.setMoveType(moveTypeValue.getText().toString());
        bean.setSpecialInventory(specialInventory.getText().toString());
        bean.setBusinessType(businessTypeValue.getText().toString());
        bean.setTargetFactoryList(mTargetFactoryList);
        ChangeStorageUtils.saveChangeStorageBean(context, bean);
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(ChangeStorageManageActivity.this, "退出后将清空所填信息", new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                finish();
            }
        }).show();
    }
}